/**
 * 
 */
package com.lib724.web.controller;

import java.util.HashMap;
import java.util.Map;

import com.google.inject.Inject;
import com.javaeedev.lightweight.mvc.ModelAndView;
import com.javaeedev.lightweight.mvc.SimpleController;
import com.javaeedev.lightweight.validate.Handle;
import com.javaeedev.lightweight.validate.Validate;
import com.javaeedev.lightweight.validate.ValidateNestedProperties;
import com.lib724.model.Manager;
import com.lib724.model.Reader;
import com.lib724.service.UserService;
import com.lib724.web.ModelAndError;

/**
 * 对登陆、读者用户的管理
 * @author xiaoli
 * 2009 上午01:30:40
 */
public class UserController extends SimpleController {

	public Manager getManager() {
		return manager;
	}

	public void setManager(Manager manager) {
		this.manager = manager;
	}

	public Reader getReader() {
		return reader;
	}

	public void setReader(Reader reader) {
		this.reader = reader;
	}

	@ValidateNestedProperties(
			{@Validate(field="username", min=4, max=16, mask="\\w+", label="帐号格式错误，4-16位字母数字符号。"),
			@Validate(field="password", min=4, max=16, mask="\\w+", label="密码格式错误，4-16位字母数字符号。")
	})
	private Manager manager;
	@ValidateNestedProperties({
			@Validate(field = "name", min = 2, max = 16, mask = "\\w{2,16}", label = "姓名格式错误，2-16位字母数字符号。"),
			@Validate(field = "mobile", required = false, min = 11, max = 13, mask = "(+)?(86)?\\d{11}", label = "手机格式错误，11位数字 如13287651234。"),
			@Validate(field = "mail", required = true, min = 5, max = 64, mask = "(\\w|_|-)+@(\\w|_|-)+(.\\w|_|-)+", label = "邮件格式错误，you@mail.com") })
	private Reader reader;
	
	@Inject
	private UserService userService;

	@Handle(view="login")
	public ModelAndView executeDefault() throws Exception {
		// TODO Auto-generated method stub
		return new ModelAndView("login");
	}

	@Handle(validate="all")
	public ModelAndView login() {
		ModelAndView m = new ModelAndView("login"); 
		if (!errors.isEmpty()) {
//			m = new ModelAndView("login", errors); 
		}
		return m;
	}
	
	/**
	 * 创建一个用户
	 * @return
	 */
	@Handle(validate="reader")
	public ModelAndView profile() {
		return new ModelAndView("profile");
	}
	
	@Handle(validate="manager")
	public ModelAndView register() {
		ModelAndError<Manager> m = userService.createManager(manager);
		Map map = new HashMap();
		map.put("manager", manager);
		return new ModelAndView("register" , map);
	}
}
